<?php
namespace services\models;

//use Yii;
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class IndexQuery{
    
    //热门搜索
    public function getSearchWord()
    {
        $data = unserialize(\Yii::$app->redis_old->get('index_hot_keywords'));
        if (!isset($data)) {
            $data = \Yii::$app->db->createCommand('select keyword,url from tmall1.search_hot_table where status=1 order by train limit 3')->queryAll();
            if(!empty($data))
            {
                \Yii::$app->redis_old->set('index_hot_keywords',  serialize($data));
            }else
            {
                \Yii::$app->redis_old->set('index_hot_keywords',  '');
                $data = [];
            } 
        }
        return $data;
    }
    
    
    //LOGO右侧广告
    public function getLogoAdv()
    {
        $logo_adv = unserialize(\Yii::$app->redis_index->get('logo_adv'));
        if(!isset($logo_adv))
        {
            $datetime = date('Y-m-d 00:00:00');
            $list = \Yii::$app->db->createCommand("SELECT * FROM d_try.`logo_adv`  where `status`=1 and '$datetime' between recommend_starttime and recommend_endtime ORDER BY add_time desc limit 1")->queryAll();
            if(!empty($list))
            {
                foreach($list as $v)
                {
                    $logo_adv[] = $v;
                }
                \Yii::$app->redis_index->set('logo_adv', serialize($logo_adv));
                \Yii::$app->redis_old->set('logo_adv', serialize($logo_adv));
            }else
            {
                \Yii::$app->redis_index->set('logo_adv', '');
                \Yii::$app->redis_old->set('logo_adv', '');
                $logo_adv = [];
            }
        }
        return $logo_adv;
    }
    
    // 获取PC banner广告信息
    public function getBannerListByRedis()
    {
        $m_now_time = time();
        $banner_list = unserialize(\Yii::$app->redis_old->get('51banner_list'));

        if(!isset($banner_list))
        {
            $list = \Yii::$app->db->createCommand("SELECT * FROM tmall1.`cycle_bestry` where isnot_display=1 and thumb<>'' order by train")->queryAll();
            if(!empty($list[0]))
            {
                foreach($list as $val)
                {
                    if($val['begintime'] <= $m_now_time && $m_now_time <= $val['endtime'])
                    {
                        $banner_list[] = $val; 
                    }
                }
                \Yii::$app->redis_old->set('51banner_list', serialize($banner_list));
            }else
            {
                \Yii::$app->redis_old->set('51banner_list', '');
                $banner_list = [];
            }
        }
        return $banner_list;
    } 
    
    // 获取WAP banner广告信息
    public function getWapBannerListByRedis()
    {
        $m_now_time = time();
        $banner_list = unserialize(\Yii::$app->redis_old->get('wap_banner_index_list'));

        if(!isset($banner_list))
        {
            $list = \Yii::$app->db->createCommand("SELECT * FROM tmall1.`wap_banner_index` where isnot_display=1 order by train asc,uid desc limit 8")->queryAll();
            if(!empty($list[0]))
            {
                foreach($list as $val)
                {
                    if($val['begintime'] <= $m_now_time && $m_now_time <= $val['endtime'])
                    {
                        $banner_list[] = $val; 
                    }
                }
                \Yii::$app->redis_old->set('wap_banner_index_list', serialize($banner_list));
            }else
            {
                \Yii::$app->redis_old->set('wap_banner_index_list', '');
                $banner_list = [];
            }
        }
        return $banner_list;
    }
    
    //大试特试、先试后买、试用报告
    public function  getTryAdv()
    {
        $try_adv_list = unserialize(\Yii::$app->redis_index->get('try_adv'));
        if(!isset($try_adv_list))
        {
            $datetime = date('Y-m-d 00:00:00');
            $result = \Yii::$app->db->createCommand("SELECT * FROM d_try.`try_adv` where `status`=1 ORDER BY type,recommend_time desc;")->queryAll();
            if(!empty($result))
            {
                $flag1 = $flag2= $flag3 = 0;
                foreach($result as $k=>$v)
                {
                    if($v['recommend_time'] >= $datetime)
                    {
                         $try_adv_list[$v['type']][$k]['pic'] = $v['pic'];
                         $try_adv_list[$v['type']][$k]['url'] = $v['url'];
                         $try_adv_list[$v['type']][$k]['recommend_time'] = $v['recommend_time'];
                     }elseif(($v['type'] == 1 && $flag1 != 1) || ($v['type'] == 2 && $flag2 != 1) || ($v['type'] == 3 && $flag3 != 1))
                     {
                         $try_adv_list[$v['type']][$k]['pic'] = $v['pic'];
                         $try_adv_list[$v['type']][$k]['url'] = $v['url'];
                         $try_adv_list[$v['type']][$k]['recommend_time'] = $v['recommend_time'];
                         $flag1 = $v['type'] == 1 ? 1 : 0;
                         $flag2 = $v['type'] == 2 ? 1 : 0;
                         $flag3 = $v['type'] == 3 ? 1 : 0;
                     }
                }
                \Yii::$app->redis_index->set('try_adv',  serialize($try_adv_list));
            }else
            {
                \Yii::$app->redis_index->set('try_adv',  '');
                $try_adv_list = [];
            }
        }    
        return $try_adv_list;
    }
    
    //每日一试
    public function getDayTry()
    {
        $day_try_list = unserialize(\Yii::$app->redis_index->get('day_try'));
        if(!isset($day_try_list))
        {
            $result = \Yii::$app->db->createCommand("select * from d_try.day_try where status=1 order by recommend_time desc;")->queryAll();
            if(!empty($result))
            {
                $flag = 0;
                $datetime = date('Y-m-d 00:00:00');
                foreach($result as $k=>$v)
                {
                    if($v['recommend_time'] >= $datetime)
                    {
                        $day_try_list[$k]['product_id'] = $v['product_id'];
                        $day_try_list[$k]['pic'] = $v['pic'];
                        $day_try_list[$k]['recommend_time'] = $v['recommend_time'];
                    }elseif($flag != 1)
                    {
                        $day_try_list[$k]['product_id'] = $v['product_id'];
                        $day_try_list[$k]['pic'] = $v['pic'];
                        $day_try_list[$k]['recommend_time'] = $v['recommend_time'];
                        $flag = 1;
                    }
                }
                \Yii::$app->redis_index->set('day_try',  serialize($day_try_list));
            }else
            {
                \Yii::$app->redis_index->set('day_try',  '');
                $day_try_list = [];
            }
            
        }   
        return $day_try_list;
    }
    
    //小试推荐
    public function getTryRec()
    {
        $try_rec_list = unserialize(\Yii::$app->redis_index->get('try_rec'));
        if(!isset($try_rec_list))
        {
            $result = \Yii::$app->db->createCommand("SELECT * FROM d_try.`try_rec` where `status`=1 ORDER BY display_position,recommend_time desc;")->queryAll();
            if(!empty($result))
            {
                $flag1 = $flag2 = $flag3 = $flag4 = 0;
                $datetime = date('Y-m-d 00:00:00');
                foreach($result as $k=>$v)
                {
                    $product_info1 = Product::findOne(['product_id'=>$v['product1']]);
                    $product_info2 = Product::findOne(['product_id'=>$v['product2']]);
                    $product_info3 = Product::findOne(['product_id'=>$v['product3']]);
                    if($v['recommend_time'] >= $datetime)
                    {
                        $try_rec_list[$v['display_position']][$k]['product_id1'] = $v['product1'];
                        $try_rec_list[$v['display_position']][$k]['product_id2'] = $v['product2'];
                        $try_rec_list[$v['display_position']][$k]['product_id3'] = $v['product3'];
                        $try_rec_list[$v['display_position']][$k]['goods_id1'] = $product_info1->goods_id;
                        $try_rec_list[$v['display_position']][$k]['goods_id2'] = $product_info2->goods_id;
                        $try_rec_list[$v['display_position']][$k]['goods_id3'] = $product_info3->goods_id;
                        $try_rec_list[$v['display_position']][$k]['pic'] = $v['pic1'];
                        $try_rec_list[$v['display_position']][$k]['recommend_time'] = $v['recommend_time'];
                    }elseif(($v['display_position'] == 1 && $flag1 != 1) || ($v['display_position'] == 2 && $flag2 != 1) || ($v['display_position'] == 3 && $flag3 != 1) || ($v['display_position'] == 4 && $flag4 != 1))
                    {
                        $try_rec_list[$v['display_position']][$k]['product_id1'] = $v['product1'];
                        $try_rec_list[$v['display_position']][$k]['product_id2'] = $v['product2'];
                        $try_rec_list[$v['display_position']][$k]['product_id3'] = $v['product3'];
                        $try_rec_list[$v['display_position']][$k]['goods_id1'] = $product_info1->goods_id;
                        $try_rec_list[$v['display_position']][$k]['goods_id2'] = $product_info2->goods_id;
                        $try_rec_list[$v['display_position']][$k]['goods_id3'] = $product_info3->goods_id;
                        $try_rec_list[$v['display_position']][$k]['pic'] = $v['pic1'];
                        $try_rec_list[$v['display_position']][$k]['recommend_time'] = $v['recommend_time'];
                        $flag1 = $v['display_position'] == 1 ? 1 : 0;
                        $flag2 = $v['display_position'] == 2 ? 1 : 0;
                        $flag3 = $v['display_position'] == 3 ? 1 : 0;
                        $flag4 = $v['display_position'] == 4 ? 1 : 0;
                    }
                }
                \Yii::$app->redis_index->set('try_rec',  serialize($try_rec_list));
            }else
            {
                \Yii::$app->redis_index->set('try_rec',  '');
                $try_rec_list = [];
            }
        }    
        return $try_rec_list;
    }
    
    public function getSuperTry()
    {
        $super_try_list = unserialize(\Yii::$app->redis_index->get('super_try'));
        if(!isset($super_try_list))
        {
            $result = \Yii::$app->db->createCommand("select * from d_try.super_try order by position,recommend_time desc")->queryAll();
            if(!empty($result))
            {
                $flag1 = $flag2= $flag3 = 0;
                $datetime = date('Y-m-d 00:00:00');
                foreach($result as $k=>$v)
                {
                    $product_info = Product::findOne(['product_id'=>$v['product']]);
                    if($v['recommend_time'] >= $datetime)
                    {
                         $super_try_list[$v['position']][$k]['product_id'] = $v['product'];
                         $super_try_list[$v['position']][$k]['goods_id'] = $product_info->goods_id;
                         $super_try_list[$v['position']][$k]['recommend_time'] = $v['recommend_time'];
                     }elseif(($v['position'] == 1 && $flag1 != 1) || ($v['position'] == 2 && $flag2 != 1) || ($v['position'] == 3 && $flag3 != 1))
                     {
                         $super_try_list[$v['position']][$k]['product_id'] = $v['product'];
                         $super_try_list[$v['position']][$k]['goods_id'] = $product_info->goods_id;
                         $super_try_list[$v['position']][$k]['recommend_time'] = $v['recommend_time'];
                         $flag1 = $v['position'] == 1 ? 1 : 0;
                         $flag2 = $v['position'] == 2 ? 1 : 0;
                         $flag3 = $v['position'] == 3 ? 1 : 0;
                     }
                }
                \Yii::$app->redis_index->set('super_try',  serialize($super_try_list));
            }else
            {
                \Yii::$app->redis_index->set('super_try',  '');
                $super_try_list= [];
            }
        }
        return $super_try_list;
    }




    public function getIndexAd($array)
    {
        if(!empty($array)){
            foreach($array as $i => $val)
            {
                
                 $ads[] = \Yii::$app->redis_old->get('liketryad_'.$array[$i]); 
                //$keys[] = 'liketryad_'.$array[$i];
            }            
            //$ads = \Yii::$app->redis_old->multiple($keys); 
            $ad_data = array();
            foreach ($ads as  $val) {
                $value = unserialize($val);
                $ad_data[$value['uid']] = $value;
            }
            return $ad_data;
        }
    }


    
    
    
    
    
    
    
    
    
    
}
